//
//  TopicViewController.m
//  XhlTrip
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TopicViewController.h"
#import "TopicDetailController.h"
#import "LeaonWorking.h"
#import "TopicModel.h"
#import "TopicViewCell.h"
#import "MBProgressHUD.h"

@interface TopicViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *topicArr;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation TopicViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.topicArr = [NSMutableArray array];
    }
    return self;
}


-(void)passV:(NSString *)str
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    NSString *path = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/articles.json?destination_id=", str, @"&page=1"];
    
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"topic", str];
    [LeaonWorking netWorkingHanderGetGataWithUrl:path pathname:nn resultBlock:^(id result) {
        [self.topicArr addObjectsFromArray:result];
        [_tableView reloadData];

        [hud removeFromSuperview];
        [hud release];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
   
}


- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.rowHeight = 200 * Height;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
  
    [self.view addSubview:self.tableView];
    [_tableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TopicViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inde"];
    if (cell == nil) {
        cell = [[TopicViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"inde"];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    TopicModel *toModel = [[TopicModel alloc] init];
    [toModel setValuesForKeysWithDictionary:self.topicArr[indexPath.row]];
    cell.model = toModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicDetailController *topicDetail = [[TopicDetailController alloc] init];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"专题";
    self.navigationItem.backBarButtonItem = backItem;
    [topicDetail passVa:[self.topicArr[indexPath.row] objectForKey:@"id"]];
    [self.navigationController pushViewController:topicDetail animated:YES];
   
    [topicDetail release];
}

- (void)dealloc
{
    [_tableView release];
    [_topicArr release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
